var common = require('../../../utils/common.js');
var app = getApp();
var baseUrl = app.url;

Page({
    data: {
        datalist:{},
        address:'',
        inputVal:"",
        __PUBLIC__:app.__PUBLIC__   
    },
    inputChange:function(event){
        var e = event.detail.value
        if(e.length>0){
           this.setData({inputVal:e})
        }
    },
    select:function (event){
    	var that = this;
    	wx.chooseLocation({
		  	success: function(res) { 
			    var latitude = res.latitude
			    var longitude = res.longitude
			    var address = res.address
			    that.setData({address:res.address,latitude:res.latitude,longitude:res.longitude})
  			}
		})
    },
    formSubmit:function (event) {
        var lng = this.data.longitude;
        var lat = this.data.latitude
        var formData = event.detail.value;
        var inputVal = this.data.inputVal;
        if(inputVal.length == 0){
            wx.showModal({
                content: '请添加名称',
                showCancel: false,
            });  
        }else{
            common.bestCurl('POST','/agency/add',res=>{
                if(res.statusCode == 200){
                    if(res.data.code == 1){
                        wx.showToast({
                            title: '添加成功',
                            icon: '',
                            duration: 5000
                        });
                        setTimeout(res=>{
                            wx.redirectTo({
                                url: '../list/list'
                            })
                        },2000)
                    }else if(res.data.code == 2){
                        wx.showModal({
                            content: '添加失败,请重新添加',
                            showCancel: false,
                        });
                    }
                }
            },Object.assign({lng:lng},{lat:lat},formData))            
        }
    }
});

